package com.chen.blue.controller.admin.system;


import com.chen.blue.converter.system.LogConverter;
import com.chen.blue.domain.query.system.LogQuery;
import com.chen.blue.domain.vo.excel.LogExcel;
import com.chen.blue.domain.vo.system.LogVo;
import com.chen.blue.exception.e.DtoNullException;
import com.chen.blue.result.DataResult;
import com.chen.blue.result.R;
import com.chen.blue.service.system.ILogService;
import com.chen.blue.utils.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author CLT
 * @since 2023-10-28
 */
@SuppressWarnings("all")
@Api(tags = "日志模块")
@RestController
@RequestMapping("/log")
public class LogController {

    @Resource
    private ILogService logService;

    @CrossOrigin
    @ApiOperation(value = "日志查询", notes = "日志查询")
    @PostMapping("query")
    public R<DataResult<List<LogVo>>> query(@RequestBody LogQuery logQuery) throws DtoNullException {

        return R.success(logService.queryLog(logQuery));

    }

    @ApiOperation(value = "ID获取日志", notes = "ID获取日志")
    @GetMapping("queryById/{id}")
    public R<DataResult<LogVo>> queryById(@PathVariable("id") Long id) throws DtoNullException {

        return R.success(logService.queryLogById(id));

    }

    @ApiOperation(value = "导出日志", notes = "导出日志")
    @GetMapping("/export")
    public void export(HttpServletResponse response) throws DtoNullException, IOException {

        DataResult<List<LogVo>> logVoDataResult = logService.queryLog(new LogQuery());
        List<LogVo> dataList = logVoDataResult.getResult();
        List<LogExcel> logExcelList = LogConverter.voToExcel(dataList);
        ExcelUtil.export(response, LogExcel.class, "日志信息.xls", "日志列表", logExcelList);

    }

    @ApiOperation(value = "导入日志", notes = "导入日志")
    @PostMapping("/import")
    public R include(@RequestParam("file") MultipartFile file, HttpServletResponse response) throws IOException {

        List<LogExcel> dataList = ExcelUtil.include(file, LogExcel.class, response);
        boolean isSuccess = logService.include(dataList);
        return isSuccess ? R.success() : R.faild(null, null);

    }

}

